<template>
  <el-dialog
      :title="props.type == 'add' ? '添加老人信息' : '修改老人信息'"
      v-model="state.visible"
      width="400px"
  >
    <el-form :model="state.ruleForm" :rules="state.rules" ref="formRef" label-width="100px" class="good-form">
      <el-form-item label="老人名字" prop="name">
        <el-input type="text" v-model="state.ruleForm.elderlyName"></el-input>
      </el-form-item>
      <el-form-item label="性别" prop="gender">
        <el-input type="text" v-model="state.ruleForm.gender"></el-input>
      </el-form-item>
      <el-form-item label="年龄" prop="age">
        <el-input type="text" v-model="state.ruleForm.age"></el-input>
      </el-form-item>
      <el-form-item label="电话" prop="phone">
        <el-input type="text" v-model="state.ruleForm.phone"></el-input>
      </el-form-item>
      <el-form-item label="地址" prop="adress">
        <el-input type="text" v-model="state.ruleForm.address"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="state.visible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
import {reactive, ref} from 'vue'
import {useRoute} from 'vue-router'
import {ClientAddElderly,ClientUpdateElderly,ClientgetElderlyInfoById} from '../../service/elderlyInfoApi.js'

import {ElMessage} from 'element-plus'
const props = defineProps({
  type: String, // 用于判断是添加还是编辑
  reload: Function // 添加或修改完后，刷新列表页
})
const formRef = ref(null)
const route = useRoute()
const state = reactive({

  visible: false,
  categoryLevel: 1,
  parentId: 0,
  ruleForm: {
    id: '',
    elderlyName: '',
    gender:'',
    age:'',
    phone:'',
    address:'',
  },
  rules: {

    elderlyName: [
      {required: true, message: '名字不能为空', trigger: ['change']}
    ],
    gender: [
      {required: true, message: '性别不能为空', trigger: ['change']}
    ],
    age: [
      {required: true, message: '年龄不能为空', trigger: ['change']}
    ],
    phone: [
      {required: true, message: '电话不能为空', trigger: ['change']}
    ],
    address: [
      {required: true, message: '地址不能为空', trigger: ['change']}
    ],


  },
})



// 获取详情
const getDetail = async (id) => {
  const result = await ClientgetElderlyInfoById(id);
  // 回显表单数据
  state.ruleForm = {
    id: result.data.id,
    elderlyName:result.data.elderlyName,
    age:result.data.age,
    gender:result.data.gender,
    phone:result.data.phone,
    address:result.data.address
  }

}

// 开启弹窗
const open = (id) => {
  state.visible = true
  if (id) {
    state.ruleForm.id = id
    // 如果是有 id 传入，证明是修改模式
    getDetail(id)
  } else {
    // 否则为新增模式
    state.ruleForm = {
      id: '',
      elderlyName:'',
      gender:'',
      age:'',
      phone:'',
      address:'',
    }

  }
}
// 关闭弹窗
const close = () => {
  state.visible = false
}
const submitForm = () => {
  formRef.value.validate(async (valid) => {
    if (valid) {
      if (props.type === 'add') {
        // 添加方法
        await ClientAddElderly(state.ruleForm).then(() => {
          ElMessage.success('添加成功')
          close()
          if (props.reload) props.reload()
        })
      } else {
        // 修改方法
        await ClientUpdateElderly(state.ruleForm).then(() => {
          ElMessage.success('修改成功')
          close()
          if (props.reload) props.reload()
        })
      }
    }
  })
}
defineExpose({open, close})
</script>

<style scoped>
.el-upload__tip {
  float: left;
}

.cover {
  border: 1px solid #FFFFFF;
  border-radius: 10px;
  width: 80px;
  height: 80px;
}
</style>